import * as types from './mutations-type';
import HbbRequest, {
  GET_BASE_URL,
  GET_APIENTID,
  GET_ENT_SYSTEM_PARAMS,
} from '@/api/api';
// import Request from '@/api/Request';
import hbbRouter from '@/router';
import {
  MINA_TYPE_NAME,
  SERVERS_APP_ID,
} from '@/config';

const actions = {
  updateParams({ commit }, params) {
    commit(types.UPDATE_PARAMS, params);
  },

  async getAPIEntID({ state, commit }) {
      if (state.APIEntID && state.APIEntID != '') {
          return false;
      }else{
          let requestData = {
              Secret: '@Secret',
              AppID: '@AppID',
              EntID: '0',
              UserID: '0',
              BSN: '@BSN'
          }; 
          const rs = await new HbbRequest(GET_APIENTID, {
              tableData: [requestData]
            }).send();
          const EntInfo = rs.data.table1[0];
          commit(types.UPDATE_APIENTID, EntInfo.EntID);
          return true;
      }
  },

  async getBaseUrl({ commit, state }) {
      if (state.baseUrl) {
        return state.baseUrl;
      } else {
        const rs = await new HbbRequest(GET_BASE_URL, {
          tableData: [
            {
              EntID: '0',
              UserID: '0',
              AppID: '@AppID',
              Secret: '@Secret',
              BSN: '@BSN'
            }
          ]
        }).send();
        const baseUrl = rs.data.table1[0];
        commit(types.UPDATE_BASE_URL, baseUrl);
        return baseUrl;
      }
  },

  async refreshEntParams({ commit, dispatch, state }) {
    const rs = await new HbbRequest(GET_ENT_SYSTEM_PARAMS, {
      tableData: [
        {
          EntID: state.params.EntID,
          UserID: state.params.UserID,
          AppID: '@AppID',
          Secret: '@Secret',
          BSN: '@BSN',
        }
      ]
    }).send();
    const entSysParams = {};
    rs.data.table1.forEach(({ ParmID, ParmValue }) => {
      entSysParams[ParmID] = ParmValue;
    });
    commit(types.UPDATE_ENT_PARAMS, entSysParams);
    return entSysParams;
  },

  

}

export default actions;